#! /bin/bash

function check_sys()
{
    if [[ -f /etc/redhat-release ]]; then
        release="centos"
    elif cat /etc/issue | grep -q -E -i "debian"; then
        release="debian"
    elif cat /etc/issue | grep -q -E -i "ubuntu"; then
        release="ubuntu"
    elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
        release="centos"
    elif cat /proc/version | grep -q -E -i "debian"; then
        release="debian"
    elif cat /proc/version | grep -q -E -i "ubuntu"; then
        release="ubuntu"
    elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
        release="centos"
    fi
    bit=$(uname -m)
        if test "$bit" != "x86_64"; then
            bit="arm64"
        else bit="amd64"
    fi
}
disable_selinux(){
    update-alternatives --set iptables /usr/sbin/iptables-legacy
    if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
        sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
        setenforce 0
    fi
}
function install(){
    rm -rf /etc/xiandan/nginx.sh
    rm -rf /etc/xiandan/nginx/xd-nginx
    check_sys
    disable_selinux
    if [ ! -f /etc/xiandan/nginx ];then
        mkdir -p /etc/xiandan/nginx
        mkdir -p /etc/xiandan/nginx/logs
    fi
    if [[ ${release} == "centos" ]];then
        yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel geoip geoip-devel -y
    else
        apt install libpcre3 libpcre3-dev zlib1g-dev libssl-dev libgeoip-dev -y
    fi
    wget sh.alhttdw.cn/xiandan/nginx.sh -O /etc/xiandan/nginx.sh
    systemctl daemon-reload
    if cat /etc/os-release |grep 'bookworm';then
        if [[ ${bit} == "amd64" ]];then
            wget --no-check-certificate sh.alhttdw.cn/xiandan/nginx/x86/debian12/nginx -O /etc/xiandan/nginx/xd-nginx
        else
        	wget --no-check-certificate sh.alhttdw.cn/xiandan/nginx/arm/nginx -O /etc/xiandan/nginx/xd-nginx
        fi
    else
        if [[ ${bit} == "amd64" ]];then
            wget --no-check-certificate sh.alhttdw.cn/xiandan/nginx/nginx -O /etc/xiandan/nginx/xd-nginx
        else
        	wget --no-check-certificate sh.alhttdw.cn/xiandan/nginx/arm/nginx -O /etc/xiandan/nginx/xd-nginx
        fi
    fi
    chmod +x /etc/xiandan/nginx/xd-nginx
    wget sh.alhttdw.cn/xiandan/nginx/common.conf -O /etc/xiandan/nginx/common.conf
    wget sh.alhttdw.cn/xiandan/nginx/client.conf -O /etc/xiandan/nginx/client.conf
    wget sh.alhttdw.cn/xiandan/nginx/server.conf -O /etc/xiandan/nginx/server.conf
    echo "/usr/local/lib64/" >> /etc/ld.so.conf
    ldconfig
}
function uninstall(){
    rm -rf /etc/xiandan/nginx
}

function client() {
	localport=$1
	sni=$2
	proxy_protocol=$3
	if [ ! -f /etc/xiandan/nginx/$localport ];then
		mkdir -p /etc/xiandan/nginx/$localport
	fi
	if [ ! -f /etc/xiandan/nginx/common.conf ];then
		wget sh.alhttdw.cn/xiandan/nginx/common.conf -O /etc/xiandan/nginx/common.conf
	fi
	if [ ! -f /etc/xiandan/nginx/client.conf ];then
		wget sh.alhttdw.cn/xiandan/nginx/client.conf -O /etc/xiandan/nginx/client.conf
	fi
	cp /etc/xiandan/nginx/client.conf /etc/xiandan/nginx/$localport/nginx.conf
	confs=(`cat /etc/xiandan/nginx/$localport/nginx$localport.conf`)
	for cell in ${confs[@]};
	do 
	item=(`echo $cell | tr '#' ' '`)
	    sed -i "/#servers_end/i\	server ${item[0]}:${item[1]};" /etc/xiandan/nginx/${localport}/nginx.conf
	    sed -i "s/#port/${localport}/g" /etc/xiandan/nginx/$localport/nginx.conf
	done
	sed -i "s/#sni/${sni}/g" /etc/xiandan/nginx/$localport/nginx.conf
# 	if [ $proxy_protocol == 'true' ];then
# 	    sed -i "s/proxy_protocol off/proxy_protocol on/g" /etc/xiandan/nginx/$localport/nginx.conf
#     fi
    if [[ $proxy_protocol == 'true' ]];then
	    sed -i "s/#proxy_protocol/proxy_protocol/g" /etc/xiandan/nginx/$localport/nginx.conf
	    sed -i "s/proxy_protocol off/proxy_protocol on/g" /etc/xiandan/nginx/$localport/nginx.conf
    else
        sed -i "s/#proxy_protocol;/;/g" /etc/xiandan/nginx/$localport/nginx.conf
    fi
	echo "配置写入完成"
	serviceCreate
}

function server(){
	localport=$1
	proxy_protocol=$2
	if [ ! -f /etc/xiandan/nginx/$localport ];then
		mkdir -p /etc/xiandan/nginx/$localport
	fi
	if [ ! -f /etc/xiandan/nginx/common.conf ];then
		wget sh.alhttdw.cn/xiandan/nginx/common.conf -O /etc/xiandan/nginx/common.conf
	fi
	if [ ! -f /etc/xiandan/nginx/server.conf ];then
		wget sh.alhttdw.cn/xiandan/nginx/server.conf -O /etc/xiandan/nginx/server.conf
	fi
	cp /etc/xiandan/nginx/server.conf /etc/xiandan/nginx/$localport/nginx.conf
	confs=(`cat /etc/xiandan/nginx/$localport/nginx$localport.conf`)
	for cell in ${confs[@]};
	do 
	item=(`echo $cell | tr '#' ' '`)
	sed -i "/#servers_end/i\	server ${item[0]}:${item[1]};" /etc/xiandan/nginx/${localport}/nginx.conf
	sed -i "s/#port/${localport}/g" /etc/xiandan/nginx/$localport/nginx.conf
	done
	if [[ $proxy_protocol == 'true' ]];then
	    sed -i "s/#proxy_protocol/ proxy_protocol/g" /etc/xiandan/nginx/$localport/nginx.conf
	    sed -i "s/proxy_protocol off/proxy_protocol on/g" /etc/xiandan/nginx/$localport/nginx.conf
    else
        sed -i "s/#proxy_protocol;/;/g" /etc/xiandan/nginx/$localport/nginx.conf
    fi
	echo "配置写入完成"
	serviceCreate
}

function serviceCreate(){
	echo "
[Unit]
Description=xiandan${localport}xiandan
After=network.target
Wants=network.target

[Service]
Type=forking
PIDFile=/etc/xiandan/nginx/$localport/nginx.pid
ExecStartPost=/bin/sleep 0.2
ExecStart=/etc/xiandan/nginx/xd-nginx -c /etc/xiandan/nginx/$localport/nginx.conf
ExecReload=/etc/xiandan/nginx/xd-nginx -s reload -c /etc/xiandan/nginx/$localport/nginx.conf
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
    " > /etc/systemd/system/xiandan${localport}xiandan.service
     systemctl daemon-reload
	service xiandan${localport}xiandan start
# 	systemctl reload xiandan${localport}xiandan
	echo '指令发送成功！服务运行状态如下'
	echo ' '
	systemctl status xiandan${localport}xiandan --no-pager
	if [[ "$autoRestart" == "true" ]];then
	    systemctl enable xiandan${localport}xiandan
     fi
}

function stop(){
	systemctl stop xiandan${1}xiandan
	systemctl enable xiandan${1}xiandan
	rm -r /etc/xiandan/nginx/$1
	rm /etc/systemd/system/xiandan${1}xiandan.service
	systemctl daemon-reload
}

if [ $# -gt 0 ] ; then
  if [ $1 == "install" ]; then
    install
  elif [ $1 == "de" ]; then
    de $2
  elif [ $1 == "uninstall" ]; then
    uninstall
  elif [ $1 == "client" ];then
    client $2 $3 $4 $5
  elif [ $1 == "server" ];then
    server $2 $3 $4 $5
  fi
else
    install;
fi